let fs = require('fs');
const path = require('path');
const resolve = (filename) => {
  return path.resolve(__dirname, filename);
};
let ReadStream = require('./ReadStream');
let WriteStream = require('./WriteStream');
let rs, ws;
rs = new ReadStream(resolve('a.txt'), {
  highWaterMark: 4
});
ws = new WriteStream(resolve('c.txt'),{
  highWaterMark: 1
});

/*rs.on('data', function (data) {
  let flag = ws.write(data/!*.toString()*!/);
  if (!flag) {
    rs.pause();
  }
});

ws.on('drain', () => {
  console.log('清空后');
  rs.resume();
});*/


/*
//传toString后的buffer，这样写入到文件里的是正常的
chunk: 1234
---: <Buffer 31 32 33 34>
chunk: 5678
---: <Buffer 35 36 37 38>
chunk: 9012
---: <Buffer 39 30 31 32>
chunk: 3456
---: <Buffer 33 34 35 36>
chunk: 7890
---: <Buffer 37 38 39 30>
chunk:

---: <Buffer 0d 0a>



//直接传buffer 这样写入到文件里的是不正常的(虽然log都是正常的)
//如果ws.on('drain' 里还开启了 console.log('清空后'); 则会严重不正常
//多次运行结果还会不一样！！QAQ
chunk: <Buffer 31 32 33 34>
---: <Buffer 31 32 33 34>
chunk: <Buffer 35 36 37 38>
---: <Buffer 35 36 37 38>
chunk: <Buffer 39 30 31 32>
---: <Buffer 39 30 31 32>
chunk: <Buffer 33 34 35 36>
---: <Buffer 33 34 35 36>
chunk: <Buffer 37 38 39 30>
---: <Buffer 37 38 39 30>
chunk: <Buffer 0d 0a>
---: <Buffer 0d 0a>
*/


//等同于上面
rs.pipe(ws);

rs = fs.createReadStream(resolve('a.txt'));
ws = fs.createWriteStream(resolve('c.txt'));

rs.pipe(ws);
